import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import org.pushingpixels.trident.Timeline;

import edu.jhu.tcj.purfuit.Game;
import edu.jhu.tcj.purfuit.Question;

public class QuestionPanel extends GamePanel {
	
	private JTextPane questionText;
	private JTextField answer1;
	private JTextField answer2;
	private JTextField answer3;
	private JTextField answer4;
	
	private boolean answer1Selected = false;
	private boolean answer2Selected = false;
	private boolean answer3Selected = false;
	private boolean answer4Selected = false;
	
	private float value = 1.0f;
	public final Timeline timeline = new Timeline(this);
	private Game game;
	SoundManager soundManager;
	public QuestionPanel(SoundManager soundManager) {
		setLayout(null);
		this.soundManager = soundManager;
		questionText = new JTextPane();
		questionText.setEditable(false);
		questionText.setText("Question:");
		questionText.setFont(new Font("Tahoma", Font.PLAIN, 25));
		questionText.setBounds(85, 11, 669, 277);
		add(questionText);

		Font answerFont = new Font("Tahoma", Font.PLAIN, 28);
		
		answer1 = new JTextField();
		answer1.setFont(answerFont);
		answer1.setText("Answer 1");
		answer1.setEditable(false);
		answer1.setBounds(459, 319, 260, 59);
		answer1.addMouseListener(this);
		add(answer1);
		answer1.setColumns(10);

		answer2 = new JTextField();
		answer2.setFont(answerFont);
		answer2.setText("Answer 2");
		answer2.setEditable(false);
		answer2.setColumns(10);
		answer2.setBounds(459, 397, 260, 59);
		answer2.addMouseListener(this);
		add(answer2);

		answer3 = new JTextField();
		answer3.setFont(answerFont);
		answer3.setText("Answer 3");
		answer3.setEditable(false);
		answer3.setColumns(10);
		answer3.setBounds(108, 319, 260, 59);
		answer3.addMouseListener(this);
		add(answer3);

		answer4 = new JTextField();
		answer4.setFont(answerFont);
		answer4.setText("Answer 4");
		answer4.setEditable(false);
		answer4.setColumns(10);
		answer4.setBounds(108, 397, 260, 59);
		answer4.addMouseListener(this);
		add(answer4);

		
		JButton btnNewButton = new JButton("Submit");
		btnNewButton.setBounds(258, 467, 314, 65);
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (answer1Selected)
				QuestionPanel.this.game.getTurn().answerQuestion(answer1.getText());
				else if (answer2Selected)
					QuestionPanel.this.game.getTurn().answerQuestion(answer2.getText());
				else if (answer3Selected)
					QuestionPanel.this.game.getTurn().answerQuestion(answer3.getText());
				else if (answer4Selected)
					QuestionPanel.this.game.getTurn().answerQuestion(answer4.getText());
			}
		});
		add(btnNewButton);
	}

	public void setValue(float newValue) {
		this.value = newValue;
		repaint();
	}
	
	public void setGame(Game game) {
		this.game = game;
	}
	
	protected void paintComponent(Graphics graphics) {
		Graphics2D g = (Graphics2D)graphics;
		super.paintComponent(g);
		g.setColor(new Color(79, 129, 189));
		g.fillOval(0, 0, 80, 80);
		g.fillOval(716, 0, 80, 80);
		g.fillOval(0, 470, 80, 80);
		g.fillOval(716, 470, 80, 80);
		g.fillRect(40, 0, 716, 600);
		g.fillRect(0, 40, 800, 470);
		
		g.setColor(Color.orange);
		g.fillOval(50, 318, 50,50);
		g.fillOval(50, 398, 50,50);
		g.fillOval(400, 318, 50,50);
		g.fillOval(400, 398, 50,50);
		g.setColor(Color.white);
		g.fillOval(20, 20, 50, 50);

		g.setColor(Color.black);
		g.setFont(new Font("Tahoma", Font.PLAIN, 28));
		g.drawString("1", 65, 351);
		g.drawString("2", 415, 351);
		g.drawString("3", 65, 438);
		g.drawString("4", 415, 438);
		g.drawString("Q", 35, 50);
		
		g.setColor(Color.white);
		g.setStroke(new BasicStroke(9));
		if (answer1Selected) {
			g.drawRect(459, 319, 260, 59);
		} else if (answer2Selected) {
			g.drawRect(459, 397, 260, 59);
		} else if (answer3Selected) {
			g.drawRect(108, 319, 260, 59);
		} else if (answer4Selected) {
			g.drawRect(108, 397, 260, 59);
		}
		
		
		
	}

	public void updateQuestion(Question question) {
		String questionStr = question.getQuestion();
		String[] incorrectAnswersForQues = question.getIncorrectAnswersForQues();
		String correctAnswer = question.getCorrectAnswer();
		int correctIndex = (int)Math.ceil(Math.random()*4);
		questionText.setText(questionStr);
		switch(correctIndex) {
			case 1:
				answer1.setText(correctAnswer);
				answer2.setText(incorrectAnswersForQues[0]);
				answer3.setText(incorrectAnswersForQues[1]);
				answer4.setText(incorrectAnswersForQues[2]);
				break;
			case 2:
				answer2.setText(correctAnswer);
				answer1.setText(incorrectAnswersForQues[0]);
				answer3.setText(incorrectAnswersForQues[1]);
				answer4.setText(incorrectAnswersForQues[2]);
				break;
			case 3:
				answer3.setText(correctAnswer);
				answer1.setText(incorrectAnswersForQues[0]);
				answer2.setText(incorrectAnswersForQues[1]);
				answer4.setText(incorrectAnswersForQues[2]);
				break;
			case 4:
				answer4.setText(correctAnswer);
				answer1.setText(incorrectAnswersForQues[0]);
				answer2.setText(incorrectAnswersForQues[1]);
				answer3.setText(incorrectAnswersForQues[2]);
				break;
		}
		answer1Selected = false;
		answer2Selected = false;
		answer3Selected = false;
		answer4Selected = false;
		repaint();
	}
	@Override
	public void keyPressed(KeyEvent ke) {
		if (ke.getKeyCode() == KeyEvent.VK_1) {
			setSelected(2);
		} else if (ke.getKeyCode() == KeyEvent.VK_2) {
			setSelected(4);
		} else if (ke.getKeyCode() == KeyEvent.VK_3) {
			setSelected(1);
		} else if (ke.getKeyCode() == KeyEvent.VK_4) {
			setSelected(3);
		}

		if(ke.getKeyCode() == KeyEvent.VK_DOWN) {
			System.out.println("down");
			if(answer1Selected && answer2Selected)
				setSelected(1);
			else if(answer1Selected)
				setSelected(3);
			else if(answer2Selected)
				setSelected(4);
			else if(answer3Selected)
				setSelected(1);
			else if(answer4Selected)
				setSelected(2);
		}

		if(ke.getKeyCode() == KeyEvent.VK_UP) {
			if(answer1Selected && answer2Selected)
				setSelected(3);
			else if(answer1Selected)
				setSelected(3);
			else if(answer2Selected)
				setSelected(4);
			else if(answer3Selected)
				setSelected(1);
			else if(answer4Selected)
				setSelected(2);
		}

		if(ke.getKeyCode() == KeyEvent.VK_LEFT) {
			if(answer1Selected && answer2Selected)
				setSelected(2);
			else if(answer1Selected)
				setSelected(2);
			else if(answer2Selected)
				setSelected(1);
			else if(answer3Selected)
				setSelected(4);
			else if(answer4Selected)
				setSelected(3);
		}

		if(ke.getKeyCode() == KeyEvent.VK_RIGHT) {
			if(answer1Selected && answer2Selected)
				setSelected(1);
			else if(answer1Selected)
				setSelected(2);
			else if(answer2Selected)
				setSelected(1);
			else if(answer3Selected)
				setSelected(4);
			else if(answer4Selected)
				setSelected(3);
		}

		if(ke.getKeyCode()== KeyEvent.VK_ENTER)
		{
	//		submit();
		}
		System.out.println(ke.getKeyCode());
	}
	
	public void setSelected(int i){
		if(i==1) {
			answer1Selected = true;
			answer2Selected = false;
			answer3Selected = false;
			answer4Selected = false;
		} else if(i==2) {
			answer1Selected = false;
			answer2Selected = true;
			answer3Selected = false;
			answer4Selected = false;
		} else if(i==3) {
			answer1Selected = false;
			answer2Selected = false;
			answer3Selected = true;
			answer4Selected = false;
		} else if(i==4) {
			answer1Selected = false;
			answer2Selected = false;
			answer3Selected = false;
			answer4Selected = true;
		}
		repaint();
		soundManager.changeSelectedSound();
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		if (arg0.getSource().equals(answer1)) {
			setSelected(1);
		} else if (arg0.getSource().equals(answer2)) {
			setSelected(2);
		} else if (arg0.getSource().equals(answer3)) {
			setSelected(3);
		} else if (arg0.getSource().equals(answer4)) {
			setSelected(4);
		}
		repaint();
	}

}
